/// 时间工具
class DateUtil {
  ///获取目前的格式化日期 返回格式 YYYY-MM-dd HH:mm:ss
  static String getLongFormatDate({bool containSecond = false}) {
    return DateTime.now()
        .toLocal()
        .toString()
        .substring(0, containSecond ? 19 : 16);
  }

  ///获取目前的格式化日期 返回格式 YYYY-MM-dd
  static String getShortFormatDate() {
    return DateTime.now().toLocal().toString().substring(0, 10);
  }

  ///DateTime 转换为 YYYY-MM-dd 或 YYYY年MM月dd日
  static String getDateTransformFormat(DateTime dateTime,
      {bool userChinese = false, bool onlyMonthAndDay = false}) {
    String dateString = dateTime.toLocal().toString().substring(0, 10);
    if (onlyMonthAndDay) {
      dateString = dateString.substring(dateString.indexOf("-") + 1);
    }
    if (userChinese && !onlyMonthAndDay) {
      dateString = dateString.replaceFirst("-", "年 ");
      dateString = dateString.replaceFirst("-", "月 ");
      dateString = "$dateString\t日";
    }
    return dateString;
  }

  ///DateTime 转换为 YYYY-MM-dd hh:mm 或 YYYY-MM-dd hh:mm:ss
  static String getDateTransformDateAndTime(DateTime dateTime,
      {bool all = true}) {
    String dateString =
        dateTime.toLocal().toString().substring(0, all ? 19 : 16);
    return dateString;
  }

  static String getTimeTransformFormat(DateTime dateTime,
      {bool userChinese = false}) {
    String timeString = dateTime
        .toLocal()
        .toString()
        .substring(10, dateTime.toLocal().toString().length - 7);
    if (userChinese) {
      timeString = timeString.replaceFirst(":", "时 ");
      timeString = "$timeString分";
    }
    return timeString;
  }

  ///YYYY-MM-dd 或 YYYY-MM-dd HH:mm 转换为 DateTime
  static DateTime getFormatTransformDateTime(String date) {
    return DateTime.parse(date.length < 11 ? "$date 00:00:00" : date);
  }

  ///获取目前的时间戳 返回格式十位数时间戳
  static int getLongDate() {
    return DateTime.now().millisecondsSinceEpoch ~/ 1000;
  }

  ///判断传入的格式化日期是否是今天
  static bool isToday(String date) {
    DateTime parse = DateTime.parse(date);
    DateTime now = DateTime.now();
    return parse.year == now.year &&
        parse.month == now.month &&
        parse.day == now.day;
  }

  ///时间戳转换为时间
  static String timestampToDate(int timestamp) {
    var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    return date.toString().substring(0, 10);
  }

  ///时间戳转换为时间 h:m, today , yesterday , y:m:d
  static String timestampToTime(int timestamp) {
    var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    var now = DateTime.now();
    if (date.year == now.year &&
        date.month == now.month &&
        date.day == now.day) {
      return date.toString().substring(10, 16);
    }
    if (date.year == now.year &&
        date.month == now.month &&
        date.day == now.day - 1) {
      return "昨天 ";
    }
    return date.toString().substring(0, 16);
  }

  static String formatDuration(int duration) {
    if (duration == 0) {
      return '0:00';
    }
    int minutes = duration ~/ 60;
    int seconds = duration % 60;
    String secondsString = seconds < 10 ? '0$seconds' : '$seconds';
    return '$minutes:$secondsString';
  }

  /// 时间戳转换 返回 HH:mm 超过一天返回 MM-dd HH:mm
  static String timestampToTimeHhMm(int timestamp) {
    var date =
        DateTime.fromMillisecondsSinceEpoch(timestamp * 1000, isUtc: false);
    var now = DateTime.now();

    if (date.year == now.year &&
        date.month == now.month &&
        date.day == now.day) {
      return date.toString().substring(11, 16);
    }
    return date.toString().substring(5, 16);
  }
}
